{
 "cells": [
  {
   "cell_type": "code",
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "# @Author   : ly\n",
    "# @Time     : 2022/7/2 15:49\n",
    "# @File     : tushare_token.py\n",
    "# @Project  : ai_quant_trade\n",
    "# Copyright (c) Personal 2022 LY\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "execution_count": 2,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 1. 安装和简介\n",
    "Tushare网址: [**https://tushare.pro/**](https://tushare.pro/)\n",
    "安装\n",
    "``` sh\n",
    "pip install tushare\n",
    "```\n",
    "**注：如果大量调用，或需要获取更详细的信息，需要付费**\n",
    "- 旧版本接口将不再维护，建议使用新接口Tushare Pro (需要注册获取token)\n",
    "- 注册后在右上角用户头像--》个人主页--》获取token\n",
    "- 如果没有缴费加入会员，很多接口都无法调用(每个接口调用需要的积分数，请查看接口文档)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2. TuShare数据\n",
    "## 2.1 从tushare_pro获取数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n0  601318.SH   20220809  41.17  41.21  40.92  40.96      41.18   -0.22   \n1  601318.SH   20220808  41.30  41.54  41.00  41.18      41.54   -0.36   \n2  601318.SH   20220805  41.29  41.68  40.98  41.54      41.08    0.46   \n\n   pct_chg        vol       amount  \n0  -0.5342  251397.03  1031147.757  \n1  -0.8666  301594.03  1242870.500  \n2   1.1198  296710.19  1224704.136  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ts_code</th>\n      <th>trade_date</th>\n      <th>open</th>\n      <th>high</th>\n      <th>low</th>\n      <th>close</th>\n      <th>pre_close</th>\n      <th>change</th>\n      <th>pct_chg</th>\n      <th>vol</th>\n      <th>amount</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>601318.SH</td>\n      <td>20220809</td>\n      <td>41.17</td>\n      <td>41.21</td>\n      <td>40.92</td>\n      <td>40.96</td>\n      <td>41.18</td>\n      <td>-0.22</td>\n      <td>-0.5342</td>\n      <td>251397.03</td>\n      <td>1031147.757</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>601318.SH</td>\n      <td>20220808</td>\n      <td>41.30</td>\n      <td>41.54</td>\n      <td>41.00</td>\n      <td>41.18</td>\n      <td>41.54</td>\n      <td>-0.36</td>\n      <td>-0.8666</td>\n      <td>301594.03</td>\n      <td>1242870.500</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>601318.SH</td>\n      <td>20220805</td>\n      <td>41.29</td>\n      <td>41.68</td>\n      <td>40.98</td>\n      <td>41.54</td>\n      <td>41.08</td>\n      <td>0.46</td>\n      <td>1.1198</td>\n      <td>296710.19</td>\n      <td>1224704.136</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tushare as ts\n",
    "import datetime\n",
    "# replace below with your token and comment my import\n",
    "# tushare_token = 'xxxx'\n",
    "from data.private.tushare_token import tushare_token\n",
    "\n",
    "# initialize api\n",
    "ts.set_token(tushare_token)\n",
    "ts_pro = ts.pro_api()\n",
    "\n",
    "# query date setting\n",
    "# date type is year-month-day\n",
    "today = datetime.date.today()\n",
    "yesterday = today - datetime.timedelta(days = 1)\n",
    "five_days_ago = today - datetime.timedelta(days = 5)\n",
    "# change date type to YearMonthDay\n",
    "yesterday = yesterday.strftime(\"%Y%m%d\")\n",
    "five_days_ago = five_days_ago.strftime(\"%Y%m%d\")\n",
    "\n",
    "# request data，中国平安: 601318.SH, date type require is: YearMonthDay\n",
    "# api website: https://tushare.pro/document/2?doc_id=27\n",
    "# !!! 警告：未复权，如果是复权请用 通用行情接口\n",
    "df = ts_pro.query('daily', ts_code='601318.SH',\n",
    "                    start_date=five_days_ago, end_date=yesterday)\n",
    "df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "explanation of each column above\n",
    "\n",
    "| 名称 |\t类型 | 描述 |\n",
    "|:-------- |:-------:| ---------:|\n",
    "| ts_code |str | 股票代码 |\n",
    "| trade_date | str | 交易日期 |\n",
    "| open | float | 开盘价 |\n",
    "| high | float | 最高价 |\n",
    "| low | float | 最低价 |\n",
    "| close | float | 收盘价 |\n",
    "| pre_close | float | 昨收价 |\n",
    "| change | float | 涨跌额 |\n",
    "| pct_chg | float | 涨跌幅 （未复权，如果是复权请用 通用行情接口 ）\n",
    "| vol | float | 成交量 （手） |\n",
    "| amount| float |成交额 （千元）|"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.2 从老接口获取数据\n",
    "此接口不稳定，连接时间有限，且不在维护"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'head'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-4-40c9d6ea99df>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      4\u001B[0m df_old = ts.get_hist_data(code='601318.SH',\n\u001B[0;32m      5\u001B[0m                     start=five_days_ago, end=yesterday, ktype='60')\n\u001B[1;32m----> 6\u001B[1;33m \u001B[0mdf_old\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mhead\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      7\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mAttributeError\u001B[0m: 'NoneType' object has no attribute 'head'"
     ]
    }
   ],
   "source": [
    "import tushare as ts\n",
    "# 请勿配置token，否则如下api无法被识别调用，会被当做pro的接口\n",
    "# ktype: 5,15, 30, 60分钟\n",
    "df_old = ts.get_hist_data(code='601318.SH',\n",
    "                    start=five_days_ago, end=yesterday, ktype='60')\n",
    "df_old.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3. 其它常用接口\n",
    "## 3.1 股票列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "     ts_code  symbol  name area industry market list_date\n0  600000.SH  600000  浦发银行   上海       银行     主板  19991110\n1  600004.SH  600004  白云机场   广东       机场     主板  20030428\n2  600006.SH  600006  东风汽车   湖北     汽车整车     主板  19990727\n3  600007.SH  600007  中国国贸   北京     园区开发     主板  19990312\n4  600008.SH  600008  首创环保   北京     环境保护     主板  20000427",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ts_code</th>\n      <th>symbol</th>\n      <th>name</th>\n      <th>area</th>\n      <th>industry</th>\n      <th>market</th>\n      <th>list_date</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>600000.SH</td>\n      <td>600000</td>\n      <td>浦发银行</td>\n      <td>上海</td>\n      <td>银行</td>\n      <td>主板</td>\n      <td>19991110</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>600004.SH</td>\n      <td>600004</td>\n      <td>白云机场</td>\n      <td>广东</td>\n      <td>机场</td>\n      <td>主板</td>\n      <td>20030428</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>600006.SH</td>\n      <td>600006</td>\n      <td>东风汽车</td>\n      <td>湖北</td>\n      <td>汽车整车</td>\n      <td>主板</td>\n      <td>19990727</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>600007.SH</td>\n      <td>600007</td>\n      <td>中国国贸</td>\n      <td>北京</td>\n      <td>园区开发</td>\n      <td>主板</td>\n      <td>19990312</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>600008.SH</td>\n      <td>600008</td>\n      <td>首创环保</td>\n      <td>北京</td>\n      <td>环境保护</td>\n      <td>主板</td>\n      <td>20000427</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# #查询当前所有正常上市交易的股票列表\n",
    "# （注册后修改个人信息）即可免费调取\n",
    "# list_status: 上市状态 L上市 D退市 P暂停上市，默认是L\n",
    "# exchange: 交易所 SSE上交所 SZSE深交所 BSE北交所\n",
    "# market: 市场类别 （主板/创业板/科创板/CDR/北交所）\n",
    "data = ts_pro.query('stock_basic', exchange='SSE', market='主板',\n",
    "                 list_status='L', fields='ts_code,symbol,name,area,industry,market,list_date')\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "print(len(data))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2 大盘指数获取"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# benchmark to get market index,\n",
    "#  000001.XSHG: Shanghai securities composite index\n",
    "#  000300.XSHG: Shanghai and Shenzhen 300 index\n",
    "\n",
    "# 权限：用户累积2000积分可调取，5000积分以上频次相对较高\n",
    "# get index data\n",
    "df = ts_pro.index_daily(ts_code='000001.XSHG',\n",
    "                        start_date='2022-08-01', end_date='2022-08-10')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3.3 股票技术因子（量化因子）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "     ts_code trade_date   macd   kdj_k   kdj_d   kdj_j\n0  600000.SH   20220520  0.026  72.966  64.718  89.460\n1  600000.SH   20220519  0.015  63.615  60.594  69.656\n2  600000.SH   20220518  0.022  67.645  59.084  84.766\n3  600000.SH   20220517  0.024  68.134  54.804  94.794\n4  600000.SH   20220516  0.013  60.309  48.139  84.649",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ts_code</th>\n      <th>trade_date</th>\n      <th>macd</th>\n      <th>kdj_k</th>\n      <th>kdj_d</th>\n      <th>kdj_j</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>600000.SH</td>\n      <td>20220520</td>\n      <td>0.026</td>\n      <td>72.966</td>\n      <td>64.718</td>\n      <td>89.460</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>600000.SH</td>\n      <td>20220519</td>\n      <td>0.015</td>\n      <td>63.615</td>\n      <td>60.594</td>\n      <td>69.656</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>600000.SH</td>\n      <td>20220518</td>\n      <td>0.022</td>\n      <td>67.645</td>\n      <td>59.084</td>\n      <td>84.766</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>600000.SH</td>\n      <td>20220517</td>\n      <td>0.024</td>\n      <td>68.134</td>\n      <td>54.804</td>\n      <td>94.794</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>600000.SH</td>\n      <td>20220516</td>\n      <td>0.013</td>\n      <td>60.309</td>\n      <td>48.139</td>\n      <td>84.649</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# https://tushare.pro/document/2?doc_id=296\n",
    "df1 = ts_pro.stk_factor(ts_code='600000.SH', start_date='20220501', end_date='20220520',\n",
    "                    fields='ts_code,trade_date,macd,kdj_k,kdj_d,kdj_j')\n",
    "df1.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "注：\n",
    "1、本接口的前复权行情是从最新一个交易日开始往前复权，跟行情软件一致。\n",
    "2、pro_bar接口的前复权是动态复权，即以end_date参数开始往前复权，与本接口会存在不一致的可能，属正常。\n",
    "3、本接口技术指标都是基于前复权价格计算。\n",
    "\n",
    "输入参数\n",
    "\n",
    "名称\t类型\t必选\t描述\n",
    "ts_code\tstr\tN\t股票代码\n",
    "trade_date\tstr\tN\t交易日期 （yyyymmdd，下同）\n",
    "start_date\tstr\tN\t开始日期\n",
    "end_date\tstr\tN\t结束日期\n",
    "\n",
    "\n",
    "输出参数\n",
    "\n",
    "名称\t类型\t默认显示\t描述\n",
    "ts_code\tstr\tY\t股票代码\n",
    "trade_date\tstr\tY\t交易日期\n",
    "close\tfloat\tY\t收盘价\n",
    "open\tfloat\tY\t开盘价\n",
    "high\tfloat\tY\t最高价\n",
    "low\tfloat\tY\t最低价\n",
    "pre_close\tfloat\tY\t昨收价\n",
    "change\tfloat\tY\t涨跌额\n",
    "pct_change\tfloat\tY\t涨跌幅\n",
    "vol\tfloat\tY\t成交量 （手）\n",
    "amount\tfloat\tY\t成交额 （千元）\n",
    "adj_factor\tfloat\tY\t复权因子\n",
    "open_hfq\tfloat\tY\t开盘价后复权\n",
    "open_qfq\tfloat\tY\t开盘价前复权\n",
    "close_hfq\tfloat\tY\t收盘价后复权\n",
    "close_qfq\tfloat\tY\t收盘价前复权\n",
    "high_hfq\tfloat\tY\t最高价后复权\n",
    "high_qfq\tfloat\tY\t最高价前复权\n",
    "low_hfq\tfloat\tY\t最低价后复权\n",
    "low_qfq\tfloat\tY\t最低价前复权\n",
    "pre_close_hfq\tfloat\tY\t昨收价后复权\n",
    "pre_close_qfq\tfloat\tY\t昨收价前复权\n",
    "macd_dif\tfloat\tY\tMCAD_DIF (基于前复权价格计算，下同)\n",
    "macd_dea\tfloat\tY\tMCAD_DEA\n",
    "macd\tfloat\tY\tMCAD\n",
    "kdj_k\tfloat\tY\tKDJ_K\n",
    "kdj_d\tfloat\tY\tKDJ_D\n",
    "kdj_j\tfloat\tY\tKDJ_J\n",
    "rsi_6\tfloat\tY\tRSI_6\n",
    "rsi_12\tfloat\tY\tRSI_12\n",
    "rsi_24\tfloat\tY\tRSI_24\n",
    "boll_upper\tfloat\tY\tBOLL_UPPER\n",
    "boll_mid\tfloat\tY\tBOLL_MID\n",
    "boll_lower\tfloat\tY\tBOLL_LOWER\n",
    "cci\tfloat\tY\tCCI"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-5ee6fb1",
   "language": "python",
   "display_name": "PyCharm (ai_quant_trade)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}